import Vue from 'vue'
import Router from 'vue-router'
import userstore from '../js/userstore'
Vue.use(Router)

//此行代码是为了解决 以下bug，路由重定向到当前地址
//Avoided redundant navigation to current location

// const originalPush = Router.prototype.push;
// Router.prototype.push = function push(location) {
//     return originalPush.call(this, location).catch(err => err)
// }
const router = new Router({
  // mode: 'history',
  routes: [{
      description: '首页访问',
      path: '/', //访问地址
      name: 'index', //命名
      redirect: '/list' //从定向
    },
    {
      description: '任务',
      path: '/task',
      name: 'task',
      component: () => import('@/views/task'),
      meta: { //添加路由的元数据
        requiresAuth: true //是否需要强制验证授权信息
      }
    },
    {
      description: '任务详情',
      path: '/detail/:id',
      name: 'detail',
      component: () => import('@/views/task/detail'),
      meta: { //添加路由的元数据
        requiresAuth: true //是否需要强制验证授权信息
      }
    },
    {
      description: '任务编辑',
      path: '/edit/:id',
      name: 'edit',
      component: () => import('@/views/task/edit'),
      meta: { //添加路由的元数据
        requiresAuth: true //是否需要强制验证授权信息
      }
    },
    {
      description: '任务页面',
      path: '/list',
      name: 'tasklist',
      component: () => import('@/views/tasklist'),
      meta: { //添加路由的元数据
        requiresAuth: true //是否需要强制验证授权信息
      },
      // children:[{
      //   description: '任务列表',
      //   path: ':status',
      //   component: () => import('@/views/task/list'),
      //   meta: { //添加路由的元数据
      //     requiresAuth: true //是否需要强制验证授权信息
      //   },
      // }]
    },
    {
      description: '注册页面',
      path: '/register',
      name: 'register',
      component: () => import('@/views/register'),
      meta: { //添加路由的元数据
        requiresAuth: false //是否需要强制验证授权信息
      },
    },
    {
      description: '忘记密码',
      path: '/rpswd',
      name: 'rpswd',
      component: () => import('@/views/forgetpswd'),
      meta: { //添加路由的元数据
        requiresAuth: false //是否需要强制验证授权信息
      },
    }, {
      description: '消息页面',
      path: '/msg',
      name: 'msg',
      component: () => import('@/views/msg'),
      meta: { //添加路由的元数据
        requiresAuth: true //是否需要强制验证授权信息
      }
    },
    {
      description: '我的好友',
      path: '/myfriend',
      name: 'myfriend',
      component: () => import('@/views/msg/myfriends'),
      meta: { //添加路由的元数据
        requiresAuth: true //是否需要强制验证授权信息
      }
    },
    {
      description: '聊天页面',
      path: '/chatmsg/:id',
      name: 'chatmsg',
      component: () => import('@/views/msg/chatmsg'),
      meta: { //添加路由的元数据
        requiresAuth: true //是否需要强制验证授权信息
      }
    }, {
      description: '用户页面',
      path: '/user',
      name: 'user',
      component: () => import('@/views/user'),
      meta: { //添加路由的元数据
        requiresAuth: true //是否需要强制验证授权信息
      }
    }, {
      description: '我的任务明细',
      path: '/mytaskdetail/:type',
      name: 'mytaskdetail',
      component: () => import('@/views/user/mytaskdetail'),
      meta: { //添加路由的元数据
        requiresAuth: true //是否需要强制验证授权信息
      }
    },
    {
      description: '系统设置页面',
      path: '/system',
      name: 'system',
      component: () => import('@/views/system/index'),
      meta: { //添加路由的元数据
        requiresAuth: true //是否需要强制验证授权信息
      }
    },
    {
      description: '密码修改页面',
      path: '/updpw',
      name: 'updpw',
      component: () => import('@/views/system/updatepassword'),
      meta: { //添加路由的元数据
        requiresAuth: true //是否需要强制验证授权信息
      }
    }, {
      description: '组织邀请',
      path: '/ov',
      name: 'ov',
      component: () => import('@/views/system/organizationinvitation'),
      meta: { //添加路由的元数据
        requiresAuth: true //是否需要强制验证授权信息
      }
    },
    {
      description: '登录',
      path: '/login',
      name: 'login',
      component: () => import('@/views/login'),
      meta: { //添加路由的元数据
        requiresAuth: false //是否需要强制验证授权信息
      }
    },
    {
      description: '跳转页面',
      path: '/callback',
      name: 'callback',
      component: () => import('@/views/callback'),
      meta: { //添加路由的元数据
        requiresAuth: false //是否需要强制验证授权信息
      }
    },
    //当以上所有的路由都匹配不到的时候，直接访问到error页面
    {
      path: '/*',
      name: 'error',
      component: () => import('@/views/error'),
      meta: { //添加路由的元数据
        requiresAuth: false //是否需要强制验证授权信息
      }
    },
  ]
});

//拦截 
router.beforeEach( (to, from, next) => {
  //匹配两个地址是否相同，相同就不进行跳转
  if (to.path == from.path) {
    return;
  }
  //验证是否匹配到路由信息 没有的话 直接跳转到404
  if (to.matched.length === 0) {
    //跳转到404页面
    next({
      name: 'error'
    });
    return;
  } else {
    if (to.matched.some(record => record.meta.requiresAuth) == true) {
      //验证cookie
      var userinfo = userstore.getUserInfo();
      if (userinfo == null) {
        next({
          path: '/login',
          query: {
            redirect: to.path.substring(1, to.path.length)
          }
        });
        return;
      }
    }
  }
  next();
})

export default router;
